package com.sftz.modules.minapp.resourceRecord.service;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import com.sftz.assets.enums.ResourcesSortEnum;
import com.sftz.modules.datacollect.DataCollectClient;
import com.sftz.modules.datacollect.analytics.dto.StudyScoreCollectDto;
import com.sftz.modules.datacollect.analytics.service.StudyMessageService;
import com.sftz.modules.minapp.resourceRecord.dto.CourseStudyDetailsDto;
import com.sftz.modules.minapp.resourceRecord.dto.PersonCourseScoreDto;
import com.uxuexi.core.db.dao.IDbDao;

/**
 * @author   DuKL  课程得分-学习得分-作业得分-考试得分
 * @Date	 2018年12月21日
 */
@IocBean
public class StudentOtherCourseScoreViewService {

	@Inject
	private IDbDao dbDao;

	@Inject
	private StudentStudyCourseViewService studentStudyCourseViewService;

	@Inject
	private StudyProgressViewService activityStudyViewService;

	@Inject
	private CourseResourceStudyViewService courseResourceStudyViewService;

	/**
	 * @author   DuKL
	 * @throws IOException 
	 * @Date	 2018年12月21日
	 * (effect:课程下学习、考试、作业、活动的得分以及进度，课程的总得分)
	 */
	public PersonCourseScoreDto findStudentScore(Integer courseId, Integer userId) {
		PersonCourseScoreDto personCourseScore = new PersonCourseScoreDto();
		//==================================学习========================
		//得分
		double calculateVAScore = studentStudyCourseViewService.calculateVAScore(courseId, userId);
		personCourseScore.setStudyScore(calculateVAScore);
		//进度
		double studyCourse = studyCourseFuBen(courseId, userId);
		personCourseScore.setStudyAlery(studyCourse);
		//==================================作业=========================
		//得分
		double calculateHwScore = studentStudyCourseViewService.calculateHwScore(courseId, userId);
		personCourseScore.setHWScore(calculateHwScore);
		//作答进度
		double homeAleryScore = activityStudyViewService.homeAleryScore(courseId, userId);
		personCourseScore.setHomeAleryScore(homeAleryScore);
		//==================================考试=========================
		//得分
		double calculatePaperScore = studentStudyCourseViewService.calculatePaperScore(courseId, userId);
		personCourseScore.setPaperScore(calculatePaperScore);
		//进度
		double paperAleryScore = activityStudyViewService.paperAleryScore(courseId, userId);
		personCourseScore.setPaperAleryScore(paperAleryScore);
		//==================================活动=========================
		//得分
		double calculateActivitScore = studentStudyCourseViewService.calculateActivitScore(courseId, userId);
		personCourseScore.setActivitScore(calculateActivitScore);
		//进度
		double activityAleryScore = activityStudyViewService.activityAleryScore(courseId, userId);
		personCourseScore.setActivitAleryScore(activityAleryScore);
		//================================课程总得分======================
		personCourseScore.setCourseScore(calculateVAScore + calculateHwScore + calculatePaperScore
				+ calculateActivitScore);
		return personCourseScore;
	}

	/**
	 * 获取学习的分数和占比
	 * @Date 2018年12月29日
	 * @author gyp
	 * @param courseId
	 * @param userId
	 */
	public PersonCourseScoreDto studentStudyScore(Integer courseId, Integer userId) {
		PersonCourseScoreDto personCourseScore = new PersonCourseScoreDto();
		//得分
		double calculateVAScore = studentStudyCourseViewService.calculateVAScore(courseId, userId);
		personCourseScore.setStudyScore(calculateVAScore);
		//进度
		double studyCourse = studyCourse(courseId, userId);
		personCourseScore.setStudyAlery(studyCourse);

		return personCourseScore;
	}

	/**
	 * @author   DuKL
	 * @throws IOException 
	 * @Date	2018年12月21日
	 * (effect:课程学习资源进度)
	 */
	@Aop("logTime")
	public double studyCourse(Integer courseId, Integer userId) {
		double studyAlery = 0;
		double aleryVidTime = 0;
		double aleryAidTime = 0;
		StudyMessageService studyTimeService = DataCollectClient.getStudyMessageService();
		//查询该课程下已观的视频Id以及观看时间--方便计算每个资源学习百分比
		List<StudyScoreCollectDto> studyVideoVid = studyTimeService.studyVideoVid(courseId, userId);
		List<StudyScoreCollectDto> studyaudioVid = studyTimeService.studyaudioVid(courseId, userId);
		for (StudyScoreCollectDto studyVideo : studyVideoVid) {
			aleryVidTime += studyVideo.getTimeCount();
		}
		for (StudyScoreCollectDto studyAudio : studyaudioVid) {
			aleryAidTime += studyAudio.getTimeCount();
		}
		//已学习时长去重
		double timeCount = aleryVidTime + aleryAidTime;
		//该课程下所有资源时长总和
		double findCourseResTime = courseResourceStudyViewService.findCourseResTime(courseId);
		if (timeCount != 0 && findCourseResTime != 0) {
			studyAlery = timeCount / findCourseResTime;
			BigDecimal bg = new BigDecimal(studyAlery).setScale(2, RoundingMode.UP);
			studyAlery = bg.doubleValue() * 100;
		}
		if (studyAlery > 100) {
			studyAlery = 100;
		}
		return studyAlery;
	}

	/**
	 * @author   DuKL
	 * @throws IOException 
	 * @Date	2018年12月21日
	 * (effect:课程学习资源进度)
	 */
	public double studyCourseAndResTime(Integer courseId, Integer userId, double findCourseResTime) {
		double studyAlery = 0;
		double aleryVidTime = 0;
		double aleryAidTime = 0;
		StudyMessageService studyTimeService = DataCollectClient.getStudyMessageService();
		//查询该课程下已观的视频Id以及观看时间--方便计算每个资源学习百分比
		List<StudyScoreCollectDto> studyVideoVid = studyTimeService.studyVideoVid(courseId, userId);
		List<StudyScoreCollectDto> studyaudioVid = studyTimeService.studyaudioVid(courseId, userId);
		for (StudyScoreCollectDto studyVideo : studyVideoVid) {
			aleryVidTime += studyVideo.getTimeCount();
		}
		for (StudyScoreCollectDto studyAudio : studyaudioVid) {
			aleryAidTime += studyAudio.getTimeCount();
		}
		//已学习时长去重
		double timeCount = aleryVidTime + aleryAidTime;

		if (timeCount != 0 && findCourseResTime != 0) {
			studyAlery = timeCount / findCourseResTime;
			BigDecimal bg = new BigDecimal(studyAlery).setScale(2, RoundingMode.UP);
			studyAlery = bg.doubleValue() * 100;
		}
		if (studyAlery > 100) {
			studyAlery = 100;
		}
		return studyAlery;
	}

	/**
	 * @author   DuKL
	 * @Date	 2018年11月15日
	 * (effect:课程学习资源进度)
	 */
	public double studyCourseFuBen(Integer courseId, Integer userId) {
		double studyAlery = 0;
		double aleryVidTime = 0;
		double aleryAidTime = 0;
		//-----------------------------------查询课程资源信息------------------------------------------
		List<CourseStudyDetailsDto> courseResources = courseResourceStudyViewService.courseResources(courseId);
		StudyMessageService studyTimeService = DataCollectClient.getStudyMessageService();
		//查询该课程下已观的视频Id以及观看时间--方便计算每个资源学习百分比
		List<StudyScoreCollectDto> studyVideoVid = studyTimeService.studyVideoVid(courseId, userId);
		List<StudyScoreCollectDto> studyaudioVid = studyTimeService.studyaudioVid(courseId, userId);
		//避免课程上架后学生学习后又下架删除一些资源而导致学习记录时长会出现误差
		for (CourseStudyDetailsDto courseStudy : courseResources) {
			//---------视频----
			if (courseStudy.getResourceType() == ResourcesSortEnum.VIDEO.intKey()) {
				for (StudyScoreCollectDto studyVideo : studyVideoVid) {
					if (courseStudy.getVid().equals(studyVideo.getVid())) {
						aleryVidTime += studyVideo.getTimeCount();
					}
				}
			}
			//---------音频-----
			if (courseStudy.getResourceType() == ResourcesSortEnum.AUDIO.intKey()) {
				for (StudyScoreCollectDto studyAudio : studyaudioVid) {
					if (courseStudy.getVid().equals(studyAudio.getAudioId())) {
						aleryAidTime += studyAudio.getTimeCount();
					}
				}
			}
		}
		//已学习时长去重
		double timeCount = aleryVidTime + aleryAidTime;
		//该课程下所有资源时长总和
		double findCourseResTime = courseResourceStudyViewService.findCourseResTime(courseId);
		if (timeCount != 0 && findCourseResTime != 0) {
			studyAlery = timeCount / findCourseResTime;
			BigDecimal bg = new BigDecimal(studyAlery).setScale(2, RoundingMode.UP);
			studyAlery = bg.doubleValue() * 100;
		}
		if (studyAlery > 100) {
			studyAlery = 100;
		}
		return studyAlery;
	}
}
